// https://www.nowcoder.com/practice/04a5560e43e24e9db4595865dc9c63a3

#include <iostream>
#include <vector>
#include <queue>
using namespace std;


struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};


class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if (root == nullptr) {
            return ans;
        }
        queue<TreeNode*> Q;
        Q.push(root);

        while (!Q.empty()) {
            const int size = Q.size();
            vector<int> tmp;
            for (int i = 0; i < size; i++) {
                tmp.push_back(Q.front()->val);
                if (Q.front()->left != nullptr)     {   Q.push(Q.front()->left);    }
                if (Q.front()->right != nullptr)    {   Q.push(Q.front()->right);   }
                Q.pop();
            }
            ans.push_back(tmp);
        }
        return ans;
    }
};